using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;


namespace Sonnensystem
{
    public class IbachSaschaSonnensystem : Microsoft.Xna.Framework.DrawableGameComponent
    {
        SpriteBatch spriteBatch;
        KeyboardState oldState; // Aus Microsoft Documentation --> http://msdn.microsoft.com/de-de/library/bb203902%28v=xnagamestudio.40%29.aspx

        Model erde, sonne, mond;
        Texture2D texErde, texSonne, texMond;
        float rotYErde = 0, rotYMond = 0, rotErde = 0, rotMond = 0, rotSonne = 0;   // Rotationswinkel
        double aErde = 0.3, aYErde = 0.01, aYMond = 0.1, aSonne = 0.002;            // Beschleunigung

        Matrix worldMatrix, viewMatrix, projectionMatrix;

        public IbachSaschaSonnensystem(Game game)
            : base(game)
        {
            Game.Content.RootDirectory = "Content";
            worldMatrix = Matrix.CreateRotationY(0.0f) * Matrix.CreateTranslation(new Vector3(0.0f, 0.0f, 0.0f)); // Rotations, Translations, Skalierung
            viewMatrix = Matrix.CreateLookAt(new Vector3(1.0f, 15.0f, 20.0f), new Vector3(1.0f, 1.0f, 1.0f), Vector3.Up); // Von -> zu -> up
            projectionMatrix = Matrix.CreatePerspectiveFieldOfView(MathHelper.ToRadians(45.0f), 800.0f / 600.0f, 1.0f, 100.0f);  // 45 = Blickweite, 800*600 Ausgabe, 2 Ebenen zwischen denen 3D Objekete angezeigt werden
        }

        public override void Initialize()
        {
            base.Initialize();
            oldState = Keyboard.GetState();
        }

        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(Game.GraphicsDevice);

            sonne = Game.Content.Load<Model>("IbachSaschaAssets/IbachSascha-monkey");
            texSonne = Game.Content.Load<Texture2D>("IbachSaschaAssets/sonne");

            erde = Game.Content.Load<Model>("IbachSaschaAssets/IbachSascha-sphere");
            texErde = Game.Content.Load<Texture2D>("IbachSaschaAssets/earth");

            mond = Game.Content.Load<Model>("IbachSaschaAssets/IbachSascha-cube");
            texMond = Game.Content.Load<Texture2D>("IbachSaschaAssets/mond");
        }

        public override void Draw(GameTime gameTime)
        {
            Game.GraphicsDevice.Clear(Color.CornflowerBlue);

            foreach (ModelMesh mesh in erde.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = worldMatrix * Matrix.CreateRotationY(rotErde) * Matrix.CreateScale(0.8f, 0.8f, 0.8f) * Matrix.CreateTranslation(8.0f, 0.0f, 0.0f) * Matrix.CreateRotationY(rotYErde);
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;

                    effect.EnableDefaultLighting(); // Beleuchtung aktivieren

                    effect.TextureEnabled = true;
                    effect.Texture = texErde;
                }
                mesh.Draw();
            }


            foreach (ModelMesh mesh in sonne.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = worldMatrix * Matrix.CreateScale(2.0f, 2.0f, 2.0f) * Matrix.CreateTranslation(0.0f, 0.0f, 0.0f) * Matrix.CreateRotationY(rotSonne);
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;

                    effect.EnableDefaultLighting();

                    effect.TextureEnabled = true;
                    effect.Texture = texSonne;
                }
                mesh.Draw();
            }

            foreach (ModelMesh mesh in mond.Meshes)
            {
                foreach (BasicEffect effect in mesh.Effects)
                {
                    effect.World = worldMatrix * Matrix.CreateRotationZ(rotMond) * Matrix.CreateScale(0.2f, 0.2f, 0.2f) * Matrix.CreateTranslation(2.5f, 0.0f, 0.0f) * Matrix.CreateRotationY(rotYMond) * Matrix.CreateTranslation(8.0f, 0.0f, 0.0f) * Matrix.CreateRotationY(rotYErde);
                    effect.View = viewMatrix;
                    effect.Projection = projectionMatrix;

                    effect.EnableDefaultLighting();

                    effect.TextureEnabled = true;
                    effect.Texture = texMond;
                }
                mesh.Draw();
            }
        }

        public override void Update(GameTime gameTime)
        {
            if (rotYErde == 360)
                rotYErde = 0;
            rotYErde += (float)aYErde;

            if (rotYMond == 360)
                rotYMond = 0;
            rotYMond += (float)aYMond;

            if (rotErde == 360)
                rotErde = 0;
            rotErde += (float)aErde;

            if (rotSonne == 360)
                rotSonne = 0;
            rotSonne += (float)aSonne;

            base.Update(gameTime);

            // User Input

            KeyboardState newState = Keyboard.GetState();
            if (newState.IsKeyDown(Keys.A))
            {
                if (!oldState.IsKeyDown(Keys.A))
                {
                    aErde = -aErde;
                    aYErde = -aYErde;
                    aYMond = -aYMond;
                    aSonne = -aSonne;
                }
            }
            oldState = newState;
        }
    }
}
